// pages/personal-ordder/personal-ordder.js
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    tabs: [],
    activeTab: 0,
    personInfo: {},
    heightLine:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const titles = [{
      name: '待确认',
      id: 1,
      orderList: []
    }, {
      name: '待发货',
      id: 2,
      orderList: []
    }, {
      name: '发货中',
      id: 3,
      orderList: []
    }, {
      name: '已收货',
      id: 4,
      orderList: []
    }, {
      name: '已取消',
      id: 5,
      orderList: []
    }];
    this.setData({
      tabs: titles,
    })
    let index = options.index;
    if (index === undefined) index = 0;
    let heightLine = app.globalData.statusBarHeight + 44;
    this.setData({ heightLine});
    let personInfo = userInfoManager.getSupplierInfo();
    this.data.personInfo = personInfo;
    this.onChange({
      detail: {
        index: index
      }
    })

  },
  onTabCLick(e) {
    let index = e.detail.index;
    if (index != null) {
      this.initData(index);
    }
  },

  onChange(e) {
    console.log(e);
    let index = e.detail.index;
    if (index != null) {
      this.initData(index);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  hidecallBack(){
    this.initData(this.data.activeTab);
  },
  /**初始化数据 */
  initData(index) {
    let indexs = parseInt(index);
    if (indexs === 4) {
      indexs = indexs + 2;
    } else {
      indexs = indexs + 1;
    }
    http.getOrderDettails(this.data.personInfo.id, indexs,true).then(res => {
      if (res.code == 0) {
        for (var i = 0; i < res.data.length; i++) {
          res.data[i].categoryslen = Object.getOwnPropertyNames(res.data[i].categorys).length;
          res.data[i].productslen = Object.getOwnPropertyNames(res.data[i].products).length;
          res.data[i].endTime = res.data[i].arrivalDateEnd.split(" ")[1];
        }
        this.setData({
          ["tabs[" + index + "].orderList"]: res.data,
          activeTab: index
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  /**进入详情 */
  orderDetail(event) {
    let orderId = event.currentTarget.dataset.order.orderId;
    wx.navigateTo({
      url: '/pages/personal-order/personal-order-detail?orderId=' + orderId,
      success: (result) => {

      },
      fail: (e) => {
        console.log(JSON.stringify(e))
      },
      complete: () => {}
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})